package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.OrderQueryPageBean;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Order;
import cn.itcast.service.OrderService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/order")
@CrossOrigin
public class OrderController {

    @Reference
    private OrderService orderService;

    @GetMapping("/findPage")
    public Result findPage(OrderQueryPageBean orderQueryPageBean){
        try {
        PageResult result= orderService.findPage(orderQueryPageBean);
        return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,result);
        }
        catch (Exception e){
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    @PostMapping("/submit")
    public Result submit(@RequestBody Order order){
        try {
            orderService.submit(order);
            return new Result(true,MessageConstant.ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDER_FAIL);
        }
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id")String id){
        try {
            Order order=orderService.findById(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,order);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDER_FAIL);
        }
    }

    @PostMapping("/arrival")
    public Result changeArrival(@RequestBody Map map){
        Object id = map.get("id");
        Integer arrival = (Integer) map.get("arrival");
        orderService.changeArrival(id,arrival);
        return new Result(true,"修改成功");
    }


}
